package org.example.user.user;


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import static org.example.LoginPage.currentUserName;

/**
 * 账号展示
 */
public class ShowUserInfoView1 extends JFrame {
    private JTable table;
    private JButton buttonAlter, buttonDelete;


    public ShowUserInfoView1() {
        setTitle("用户端个人信息查看页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("用户id");
        model.addColumn("用户名");
        model.addColumn("用户密码");
        model.addColumn("用户电话");
        model.addColumn("用户电子邮箱");


        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            String sql = "SELECT * FROM air_user WHERE username=" + "'" + currentUserName + "'";
            ResultSet resultSet1 = statement.executeQuery(sql);

            while (resultSet1.next()) {
                int userId = resultSet1.getInt("user_id");
                String username = resultSet1.getString("username");
                String password = resultSet1.getString("password");
                String tel = resultSet1.getString("telephone");
                String email = resultSet1.getString("email");

                Object[] rowData = {userId, username, password, tel, email};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet1.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAlter = new JButton("修改数据");
        buttonDelete = new JButton("账号注销");
        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAlter);
        Panel.add(buttonDelete);
        add(Panel, BorderLayout.SOUTH);

        // 修改功能监听器
        buttonAlter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转打开新的窗口,不用关闭原窗口,同时展示
//                dispose();
                SwingUtilities.invokeLater(() -> {
                    updateUserInfoView updateUserInfoForm = new updateUserInfoView();
                    updateUserInfoForm.setSize(380, 250);
                    updateUserInfoForm.setVisible(true);
                });
            }
        });
        //注销
        buttonDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转打开新的窗口,不用关闭原窗口,同时展示
//                dispose();
                SwingUtilities.invokeLater(() -> {
                    deleteUserView deleteUserForm = new deleteUserView();
                    deleteUserForm.setSize(400, 300);
                    deleteUserForm.setVisible(true);
                });
            }
        });
    }

//    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            ShowUserInfoView1 showUserInfo1From = new ShowUserInfoView1();
//            showUserInfo1From.setSize(300, 100);
//            showUserInfo1From.setVisible(true);
//        });
//    }
}